################################################################
# Dynamic DiD with Covariates: Visas Haiti and Crime Concerns
################################################################

rm(list=ls())

library(Hmisc)
library(readstata13)
library(foreign)
library(tidyverse)
library(did)
library(ggplot2)
set.seed(1111)

#sink("04_results_concerns_haiti_covariates.txt")

############################
# Read and prepare data 
############################

# load data
load("final_county_2023april6.RData")
names(d_county)

#######################
# Results 
#######################

# d-did
out4 <- att_gt(yname = "crime_concerns_s",
               gname = "first.treat_haiti",
               idname = "county",
               tname = "survey",
               xformla = ~turnout_2013 + income_2003 + population_2011 + right_voteshare_2013,
               data = d_county)

es4 <- aggte(out4, type = "dynamic",na.rm = TRUE)
summary(es4)
ggdid(es4)

# save results for overall effect (Table A17)
pe4 <- es4$overall.att
se4 <- es4$overall.se
results4 <- rbind(pe4,se4) 
results4
write.table(results4, "overallATT_4.txt", sep="\t")

# table a19
sink(file = "tableA19.txt")
summary(es4)
sink(file = NULL)

sink()
